package com.alogic.swagger;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * 根结点
 *
 * @since 1.6.13.7 [20200812 duanyy] <br>
 */
public class SwaggerRoot extends Namespace {
    private boolean extend = false;
    private String $tag = "swagger";
    private String $version = "3.0.0";

    public SwaggerRoot(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);

        extend = PropertiesConstants.getBoolean(p,"extend",extend);
        $tag = PropertiesConstants.getRaw(p,"tag",$tag);
        $version = PropertiesConstants.getRaw(p,"version",$version);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        if (extend){
            super.onExecute(root,current,ctx,watcher);
        }else{
            String tag = PropertiesConstants.transform(ctx,$tag,"swagger");
            if (StringUtils.isNotEmpty(tag)){
                XsObject newCurrent = current.getObjectChild(tag,true);
                newCurrent.addProperty("openapi",PropertiesConstants.transform(ctx,$version,"3.0"));
                super.onExecute(root,newCurrent,ctx,watcher);
            }
        }
    }
}
